<?php

$params = call_user_func_array(['yii\helpers\ArrayHelper', 'merge'], [
    require __DIR__ . '/../../../common/config/params.php',
    require __DIR__ . '/../../../common/config/params-local.php',
    require __DIR__ . '/params.php',
    require __DIR__ . '/params-local.php'
]);

$appendName = basename(dirname(__DIR__));

return [
    'id' => 'app-' . $appendName,
    'name' => '华时乐教中心',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'apiend\controllers',
    'components' => [
        'request' => [
            'csrfParam' => '_csrf-' . $appendName,
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ],
        ],
        'response' => [
            'class' => 'yii\web\Response',
            'format' => \yii\web\Response::FORMAT_JSON,
            'on beforeSend' => function ($event) {
                $sender = $event->sender;
                $responseData = [
                    'code' => $sender->statusCode,
                    'msg' => 'Success',
                ];
                if ($sender->statusCode == 200) {
                    $responseData['data'] = $sender->data;
                } else {
                    $responseData['msg'] = $sender->data['message'];
                }

                if (YII_DEBUG) {
                    $request = Yii::$app->getRequest();
                    $debug = [
                        'url' => $request->getUrl(),
                        'method' => $request->getMethod(),
                        'queryParams' => $request->getQueryParams(),
                        'bodyParams' => $request->getBodyParams(),
                    ];
                    $headers = $request->getHeaders()->toArray();
                    foreach ($headers as $key => $vls) {
                        $headers[$key] = $vls[0];
                    }
                    $debug['headers'] = $headers;
                    $responseData['_debug'] = $debug;
                    Yii::error($debug);
                }

                $sender->statusCode = 200;
                $sender->data = $responseData;
            },
        ],
        'user' => [
            'identityClass' => 'apiend\models\User',
            'enableAutoLogin' => true,
            'enableSession' => false,
            'loginUrl' => null
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            //'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => require __DIR__ . '/restful.php',
        ],
    ],
    'modules' => require(__DIR__ . '/modules.php'),
    'params' => $params,
];
